package de.mhu.mandalaui.actions;

import java.io.PrintWriter;
import java.util.Collections;

import de.mhu.mandalaui.Component;
import de.mhu.mandalaui.Response;
import de.mhu.mandalaui.bind.Call;
import de.mhu.mandalaui.bind.Element;

public class ClickAction extends Action {
	
	private Call call = new Call();
	
	public ClickAction() {
	}
	
	public ClickAction(Action action, boolean asArray, Element ... parameters) {
		call.setAction(action);
		call.setArrayParameter(asArray);
		if (parameters != null) 
			Collections.addAll(call.getParameters(), parameters);
	}
	
	public void renderBindCall(PrintWriter out) {
//		out.print(getId() + ".bind($data,'"+ (owner == null ? "" : owner.getId())+"')");
		out.print( "function(data, event) { ");
		if (needFunctionImplementation())
			renderFunctionCall(out);
		call.renderFunctionCall(out,true);
		out.print("}");
		
	}

	public void startRender(Response response) {
		super.startRender(response);
		call.startRender(response);
	}


	@Override
	public void renderFunctionImplementation(PrintWriter out) {
	}
	
	public boolean needFunctionImplementation() {
		return false;
	}

	public Call getCall() {
		return call;
	}

	public void setCall(Call call) {
		if (call != null) call.setOwner(getOwner());
		this.call = call;
	}
	
	public void setOwner(Component owner) {
		super.setOwner(owner);
		if (call != null) call.setOwner(getOwner());
	}

}
